package edu.ccut.saturn.manager.bundles.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.ccut.saturn.component.*;

public class QueryComponentInfoByFullKey extends DefaultComponent {
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		String fullKey = parameterContext.getParameterValue(0).toString();
		
		String [] str = fullKey.split("::");

		ComponentManager componentManager = ComponentManager.getInstance();
		IComponentInfo componentInfo = (IComponentInfo) componentManager.getComponentInfo(fullKey);
		if(componentInfo == null) {
		 componentInfo = (IComponentInfo) componentManager.getComponentInfo(str[0],fullKey);
		}
		List<Map<String,String>> componentInfoList = new ArrayList<Map<String,String>>();
		
		if (componentInfo != null) {	
			Map<String,String> componentMap = new HashMap<String, String>();
			
			componentMap.put("key", componentInfo.getKey()); //$NON-NLS-1$
			componentMap.put("name", componentInfo.getName()); //$NON-NLS-1$
			componentMap.put("class", componentInfo.getClass().toString()); //$NON-NLS-1$
			componentMap.put("projectname", componentInfo.getBundleSymbolicName()); //$NON-NLS-1$
			componentMap.put("fullKey", componentInfo.getFullKey()); //$NON-NLS-1$
			componentMap.put("type", "component"); //$NON-NLS-1$ //$NON-NLS-2$
			componentMap.put("description", componentInfo.getDescription()); //$NON-NLS-1$
			
			componentInfoList.add(componentMap);
			parameterContext.setParameter(1, componentInfoList);		
			this.getComponentParamInfo(componentInfo,parameterContext);			
		}
		
	}

	private void getComponentParamInfo(IComponentInfo componentInfo, IParameterContext parameterContext) {
		if(componentInfo.getParameterInfos() != null) {			
			List<Map<String,String>> componentParamInfoList = new ArrayList<Map<String,String>>();
			
			for(IParameterInfo paramInfo :componentInfo.getParameterInfos()){
				Map<String,String> componentParamMap = new HashMap<String, String>();
				
				componentParamMap.put("id",paramInfo.getId()); //$NON-NLS-1$
				componentParamMap.put("name",paramInfo.getName()); //$NON-NLS-1$
				
				String passingType = null;
				if("in".equalsIgnoreCase(paramInfo.getPassingType())) { //$NON-NLS-1$
					passingType = Messages.getString("QueryComponentInfoByFullKey.11"); //$NON-NLS-1$
				}
				if("out".equalsIgnoreCase(paramInfo.getPassingType())) { //$NON-NLS-1$
					passingType = Messages.getString("QueryComponentInfoByFullKey.13"); //$NON-NLS-1$
				}
				if("in-out".equalsIgnoreCase(paramInfo.getPassingType())) { //$NON-NLS-1$
					passingType = Messages.getString("QueryComponentInfoByFullKey.15"); //$NON-NLS-1$
				}
				if("undefine".equalsIgnoreCase(paramInfo.getPassingType())) { //$NON-NLS-1$
					passingType = Messages.getString("QueryComponentInfoByFullKey.17"); //$NON-NLS-1$
				}
				
				componentParamMap.put("passingType",passingType); //$NON-NLS-1$
				componentParamMap.put("classType",paramInfo.getClassType().toString()); //$NON-NLS-1$
				componentParamMap.put("description",paramInfo.getDescription()); //$NON-NLS-1$
				componentParamMap.put("valueKey",paramInfo.getValueKey()); //$NON-NLS-1$
				
				String scope = null;
				if(paramInfo.getScope() == 1) {
					scope = "logic"; //$NON-NLS-1$
				} 
				
				if(paramInfo.getScope() == 2) {
					scope = "request"; //$NON-NLS-1$
				}
				if(paramInfo.getScope() == 5) {
					scope = "session"; //$NON-NLS-1$
				} 
				
				if(paramInfo.getScope() == 10) {
					scope = "application"; //$NON-NLS-1$
				}										
				componentParamMap.put("scope",scope); //$NON-NLS-1$
				componentParamInfoList.add(componentParamMap);
			}
			
			parameterContext.setParameter(2, componentParamInfoList);
		}
	}
}
